﻿<UserControl x:Class="Hopscotch.Presentation.TradeModule.Views.TradeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"	
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:prism="http://www.codeplex.com/prism"
             xmlns:cmn="clr-namespace:Hopscotch.Presentation.Common;assembly=Hopscotch.Presentation.Common"
             xmlns:cmnConst="clr-namespace:Hopscotch.Presentation.Common.Constants;assembly=Hopscotch.Presentation.Common"
             xmlns:cmnCnvrtrs="clr-namespace:Hopscotch.Presentation.Common.Converters;assembly=Hopscotch.Presentation.Common"
             xmlns:cmnData="clr-namespace:Hopscotch.Presentation.Common.Data;assembly=Hopscotch.Presentation.Common"			 
             xmlns:cmnTrgrs="clr-namespace:Hopscotch.Presentation.Common.TriggerActions;assembly=Hopscotch.Presentation.Common"
             xmlns:cntrlsCore="clr-namespace:Hopscotch.Presentation.Controls.Core;assembly=Hopscotch.Presentation.Controls"
             xmlns:cntrlsViews="clr-namespace:Hopscotch.Presentation.Controls.Views;assembly=Hopscotch.Presentation.Controls"
             xmlns:tradeVms="clr-namespace:Hopscotch.Presentation.TradeModule.ViewModels;assembly=Hopscotch.Presentation.TradeModule"			 
             
             Width="Auto"
             mc:Ignorable="d" 
             d:DesignHeight="280" d:DesignWidth="500"
             d:DataContext="{d:DesignInstance Type=tradeVms:TradeViewModel, IsDesignTimeCreatable=True}">

    <!--#region Resources-->
    <UserControl.Resources>
        <sys:Boolean x:Key="FalseValue">False</sys:Boolean>
        <sys:Boolean x:Key="TrueValue">True</sys:Boolean>

        <cmnCnvrtrs:ToolEditModeToBrushConverter x:Key="ToolEditModeToBrushConverter"/>
        <cmnCnvrtrs:FieldEditModeToBrushConverter x:Key="FieldEditModeToBrushConverter"/>
        
        <!--InteropReact/Image map-->
        <x:Array x:Key="InteropReactStatesValues" Type="cmnData:EnumImageItem">
            <cmnData:EnumImageItem EnumItem="{x:Static cmnConst:InteropReactState.Green}" 
                                           ImageUri="pack://application:,,,/Hopscotch.Presentation.Common;component/Images/react16grn.png"/>
            <cmnData:EnumImageItem EnumItem="{x:Static cmnConst:InteropReactState.Amber}" 
                                           ImageUri="pack://application:,,,/Hopscotch.Presentation.Common;component/Images/react16ylw.png"/>
            <cmnData:EnumImageItem EnumItem="{x:Static cmnConst:InteropReactState.Red}" 
                                           ImageUri="pack://application:,,,/Hopscotch.Presentation.Common;component/Images/react16red.png"/>
        </x:Array>

        <!--textBoxErrorStyle: -->
        <!--zxzx:todo, remove and replace with custom control-->
        <Style x:Key="textBoxErrorStyle" TargetType="{x:Type TextBox}">
            <Setter Property="FontSize" Value="12"/>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <!-- buttonStyle: Grey out the Button if it is disabled-->
        <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Opacity" Value="0.5" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <!--InteropReactStatesValuesProvider-->
        <ObjectDataProvider x:Key="TradeActivityValuesProvider" 
                        MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="cmnConst:InteropReactState" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

    </UserControl.Resources>
    <!--#endregion-->

    <!--#region Interaction Triggers-->
    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding ErrorNotificationRequest, Mode=OneWay}">
            <cmnTrgrs:OverlayWindowAction TargetElement="{Binding Source={x:Reference overlayTarget}}" >
                <cmnTrgrs:OverlayWindowAction.WindowContent>
                    <cntrlsViews:NotificationView />
                </cmnTrgrs:OverlayWindowAction.WindowContent>
            </cmnTrgrs:OverlayWindowAction>
        </prism:InteractionRequestTrigger>

        <!-- IsBusy Data Trigger -->
        <ei:DataTrigger Binding="{Binding Path=IsBusy}" Value="{StaticResource TrueValue}" >
            <cmnTrgrs:OverlayWindowAction TargetElement="{Binding Source={x:Reference overlayTarget}}" >
                <cmnTrgrs:OverlayWindowAction.WindowContent>
                    <cntrlsViews:BusyView BusyMessage="Saving..." />
                </cmnTrgrs:OverlayWindowAction.WindowContent>
            </cmnTrgrs:OverlayWindowAction>
        </ei:DataTrigger>
        <ei:DataTrigger Binding="{Binding Path=IsBusy}" Value="{StaticResource FalseValue}">
            <cmnTrgrs:OverlayWindowAction TargetElement="{Binding ElementName=overlayTarget}" WindowContent="{x:Null}" />
        </ei:DataTrigger>
    </i:Interaction.Triggers>
    <!--#endregion-->

    <AdornerDecorator><!--NB: This is to stop the adorder layer disappearing in tab controls-->
        
        <Grid ShowGridLines="False" x:Name="overlayTarget">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*" MinWidth="100" />
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*" MinWidth="100" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto" SharedSizeGroup="RowSize"/>
                <RowDefinition Height="Auto" SharedSizeGroup="RowSize"/>
                <RowDefinition Height="Auto" SharedSizeGroup="RowSize"/>
                <RowDefinition Height="Auto" SharedSizeGroup="RowSize"/>
                <RowDefinition Height="Auto" SharedSizeGroup="RowSize"/>
                <RowDefinition Height="Auto" SharedSizeGroup="RowSize"/>
            </Grid.RowDefinitions>

            <!--Toolbar-->
            <ToolBarTray Grid.Row="0" Grid.ColumnSpan="4" IsLocked="False" AllowDrop="True">
                <ToolBar Band="1" BandIndex="1">

                    <!--Interop Combobox-->
                    <ComboBox ItemsSource="{Binding Source={StaticResource InteropReactStatesValues}}"
                              SelectedValuePath="EnumItem"
                              SelectedValue="{Binding SelectedInteropReactState, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                              IsEnabled="{Binding Path=IsInteropReactEnabled}"
                              IsSynchronizedWithCurrentItem="True"
                              ToolTip="Interop Reaction state">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <Image Source="{Binding Path=ImageUri}"/>
                                <!--ZXZX:Todo set opacity if disabled-->
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <Button Command="{Binding NewEntityCommand}" Style="{StaticResource buttonStyle}">
                        <Image Source="pack://application:,,,/Hopscotch.Presentation.Common;component/Images/new16.png" ToolTip="New Trade"/>
                    </Button>
                    <Button Command="{Binding SaveEntityCommand}" Style="{StaticResource buttonStyle}" VerticalAlignment="Bottom"
                            ToolTipService.ShowOnDisabled="True">
                        <Image Source="pack://application:,,,/Hopscotch.Presentation.Common;component/Images/save16.png" ToolTip="Save Trade"/>
                    </Button>
                    <Button Command="{Binding CancelEditCommand}" Style="{StaticResource buttonStyle}">
                        <Image Source="pack://application:,,,/Hopscotch.Presentation.Common;component/Images/cancel16.png"  ToolTip="Cancel Edit"/>
                    </Button>
                    <Button Command="{Binding DeleteEntityCommand}" Style="{StaticResource buttonStyle}">
                        <Image Source="pack://application:,,,/Hopscotch.Presentation.Common;component/Images/delete16.png" ToolTip="Delete"/>
                    </Button>
                    <Button Command="{Binding EditLayoutCommand}" Style="{StaticResource buttonStyle}">
                        <Image Source="pack://application:,,,/Hopscotch.Presentation.Common;component/Images/cog16.png" ToolTip="Edit Layout"/>
                    </Button>
                </ToolBar>
            </ToolBarTray>

            <!--Trade Id-->
            <TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" Text="Id: " />
            <TextBox Grid.Row="1" Grid.Column="1" 
                     Text="{Binding Path=TradeId, Mode=OneWay}" 
                     Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}"
                     IsReadOnly="True" />

            <!--Instrument-->
            <TextBlock Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" Text="Instrument: " />
            <cntrlsCore:InstrumentControl
                Grid.Row="1" Grid.Column="3"
                Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}"
                Instrument="{Binding Path=Instrument, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnNotifyDataErrors=True}"    
                DataProvider="{Binding Path=InstrumentDataProvider}" />

            <!--Quantity-->
            <TextBlock Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" Text="Quantity: "/>
            <cntrlsCore:QuantityControl Grid.Row="2" Grid.Column="3"
                                        Name="QuantityControl"
                                        Validation.ErrorTemplate="{x:Null}"
                                        Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}"
                                        Quantity="{Binding Path=Quantity, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnNotifyDataErrors=True}"
                                        Increment="{Binding Path=LotSize}"/>

            <!--Activity-->
            <TextBlock Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" Text="Activity: "/>
            <ComboBox Grid.Row="2" Grid.Column="1"
                      Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}"
                      ItemsSource="{Binding Path=TradeActivityValues}"						
                      SelectedValue="{Binding TradeActivity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      IsSynchronizedWithCurrentItem="True">
            </ComboBox>

            <!--Price-->
            <TextBlock Grid.Row="3" Grid.Column="2" VerticalAlignment="Center" Text="Price: "/>
            <cntrlsCore:PriceControl Grid.Row="3" Grid.Column="3"	                                     
                Price="{Binding Path=Price, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnNotifyDataErrors=True}"
                CurrencyCode="{Binding Path=TradeCurrency, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnNotifyDataErrors=True}"
                DataProvider="{Binding Path=PriceControlDataProvider}"
                Increment="{Binding Path=TickSize}">
                <cntrlsCore:PriceControl.Background>
                    <MultiBinding Converter="{cmnCnvrtrs:FieldEditModeToBrushConverter}">
                        <Binding Path="{}"/>
                        <Binding Source="Price"/>
                        <Binding Path="Price" />
                        <Binding Path="CurrencyCode" />
                    </MultiBinding>
                </cntrlsCore:PriceControl.Background>
            </cntrlsCore:PriceControl>

            <!--TradeDate-->
            <TextBlock  Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" Text="Trade Date: "/>
            <DatePicker Grid.Row="3" Grid.Column="1"
                        Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}">
                <DatePicker.SelectedDate>
                    <Binding Path="TradeDate" Mode="TwoWay" ValidatesOnDataErrors="True" NotifyOnValidationError="True"/>
                </DatePicker.SelectedDate>
            </DatePicker>

            <!--Commission-->
            <TextBlock Grid.Row="4" Grid.Column="2" VerticalAlignment="Center" Text="Commission: "/>
            <TextBox Grid.Row="4" Grid.Column="3" 
                     Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}"
                     TextWrapping="Wrap"
                     Style="{StaticResource ResourceKey=textBoxErrorStyle}" >
                <TextBox.Text>
                    <Binding Path="Commission" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" 
                                 ValidatesOnDataErrors="True" NotifyOnValidationError="True">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

            <!--ValueDate-->
            <TextBlock  Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" Text="Value Date: "/>
            <DatePicker Grid.Row="4" Grid.Column="1"
                        Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}">
                <DatePicker.SelectedDate>
                    <Binding Path="ValueDate" Mode="TwoWay" ValidatesOnDataErrors="True" NotifyOnValidationError="True"/>
                </DatePicker.SelectedDate>
            </DatePicker>

            <!--Total-->
            <TextBlock  Grid.Row="5" Grid.Column="2" VerticalAlignment="Center" Text="Total: "/>
            <TextBox Grid.Row="5" Grid.Column="3" 
                     Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}"
                     TextWrapping="Wrap"
                     Style="{StaticResource ResourceKey=textBoxErrorStyle}" >
                <TextBox.Text>
                    <Binding Path="Total" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" 
                                 ValidatesOnDataErrors="True" NotifyOnValidationError="True">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

            <!--Account-->
            <TextBlock Grid.Row="5" Grid.Column="0" VerticalAlignment="Center" Text="Account: " />
            <TextBox Grid.Row="5" Grid.Column="1"	
                     Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}"
                     Style="{StaticResource ResourceKey=textBoxErrorStyle}">
                <TextBox.Text>
                    <Binding Path="Account" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" 
                                 ValidatesOnDataErrors="True" NotifyOnValidationError="True">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

            <!--Memo-->
            <TextBlock  Grid.Row="6" Grid.Column="0" VerticalAlignment="Center" Text="Memo: " />
            <TextBox Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="3" 
                     Background="{Binding Path=ToolEditMode, Converter={StaticResource ToolEditModeToBrushConverter}}"
                     TextWrapping="Wrap"
                     Style="{StaticResource ResourceKey=textBoxErrorStyle}">
                <TextBox.Text>
                    <Binding Path="Memo" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" 
                                 ValidatesOnDataErrors="True" NotifyOnValidationError="True">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

        </Grid>
    </AdornerDecorator>
</UserControl>
